package DisruptorTest;

import com.lmax.disruptor.BusySpinWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.Executors;

public class MessageEventMain {
    public static void main(String[] args) throws InterruptedException {
        // 构建Disrutor对象
        Disruptor<MessageEvent> disruptor = new Disruptor<MessageEvent>(new MessageEventFactory(),
                1024 * 1024,
                Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()),
                ProducerType.SINGLE,
                new BusySpinWaitStrategy()
        );

        // 设置消费者
        disruptor.handleEventsWith(new MessageEventHandler());

        // 启动Disrutor
        disruptor.start();


        RingBuffer<MessageEvent> ringBuffer = disruptor.getRingBuffer();
        MessageEventProducerWithTranslator producer = new MessageEventProducerWithTranslator(ringBuffer);

        for (int i = 0; i < 9999; i++) {
            producer.onData("哈哈" + i);
            Thread.sleep(1000);
        }

    }
}